{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sign of the Product of an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: arraySign"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组元素积的符号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>已知函数 <code>signFunc(x)</code> 将会根据 <code>x</code> 的正负返回特定值：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果 <code>x</code> 是正数，返回 <code>1</code> 。</li>\n",
    "\t<li>如果 <code>x</code> 是负数，返回 <code>-1</code> 。</li>\n",
    "\t<li>如果 <code>x</code> 是等于 <code>0</code> ，返回 <code>0</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个整数数组 <code>nums</code> 。令 <code>product</code> 为数组 <code>nums</code> 中所有元素值的乘积。</p>\n",
    "\n",
    "<p>返回 <code>signFunc(product)</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-1,-2,-3,-4,3,2,1]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>数组中所有值的乘积是 144 ，且 signFunc(144) = 1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,5,0,2,-3]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>数组中所有值的乘积是 0 ，且 signFunc(0) = 0\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-1,1,-1,1,-1]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>数组中所有值的乘积是 -1 ，且 signFunc(-1) = -1\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= nums.length <= 1000</code></li>\n",
    "\t<li><code>-100 <= nums[i] <= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sign-of-the-product-of-an-array](https://leetcode.cn/problems/sign-of-the-product-of-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sign-of-the-product-of-an-array](https://leetcode.cn/problems/sign-of-the-product-of-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[-1,-2,-3,-4,3,2,1]', '[1,5,0,2,-3]', '[-1,1,-1,1,-1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        sign = 0\n",
    "        for i in nums:\n",
    "            if i < 0:\n",
    "                sign += 1\n",
    "            elif i == 0:\n",
    "                return 0\n",
    "        if sign % 2 == 0:\n",
    "            return 1\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        weight = 1\n",
    "        for j in nums:\n",
    "            weight*=j\n",
    "        if weight == 0:\n",
    "            return 0\n",
    "        elif weight >0:\n",
    "            return 1\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        sign = 1\n",
    "        for num in nums:\n",
    "            if num == 0:\n",
    "                return 0\n",
    "            if num < 0:\n",
    "                sign = -sign\n",
    "        return sign\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        if 0 in nums: return 0\n",
    "        from functools import reduce\n",
    "        return 1 if reduce(lambda x, y:x * y,nums) > 0 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        n = 1\n",
    "        for i in nums:\n",
    "            n *= i\n",
    "        if n>0:\n",
    "            return(1)\n",
    "        elif n<0:\n",
    "            return(-1)\n",
    "        else:\n",
    "            return(0)\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: list[int]) -> int:\n",
    "        def signFunc(x):\n",
    "            if x==0:\n",
    "                return 0\n",
    "            elif x>0:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "        product=1\n",
    "        for i in nums:\n",
    "            product*=i\n",
    "            print(product)\n",
    "        return signFunc(product)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        if 0 in nums:\n",
    "            return 0\n",
    "            \n",
    "        for i in range(1, len(nums)):\n",
    "            nums[i] = nums[i-1] * nums[i]\n",
    "        \n",
    "        if nums[-1] >= 1:\n",
    "            return 1\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        x = len(nums)\n",
    "        res = 1\n",
    "        for i in range(x):\n",
    "            res *= nums[i]\n",
    "        if res > 0:\n",
    "            return 1\n",
    "        elif res == 0:\n",
    "            return 0\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        res=1\n",
    "        for i in nums:\n",
    "            if i==0:\n",
    "                return 0\n",
    "            if i<0:\n",
    "                res=-res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arraySign(self, nums: List[int]) -> int:\n",
    "        ans=1\n",
    "        for i in nums:\n",
    "            ans*=i\n",
    "        if ans==0:\n",
    "            return 0\n",
    "        elif ans>0:\n",
    "            return 1\n",
    "        else:\n",
    "            return -1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
